package com.wty.swhn.wty.view;

import java.io.ByteArrayOutputStream;

import com.wty.swhn.wty.R;
import com.wty.swhn.wty.biz.implAsmackImpl.RegisterBiz;
import com.wty.swhn.wty.model.UserEntity;
import com.wty.swhn.wty.util.Const;
import com.wty.swhn.wty.util.Tools;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Created by swhn on 2016/8/4.
 */
public class RegisterActivity extends BaseActivity {
	public final static int MSG_REGISTER = 1;
	public final static String TAG = "RegisterActiviry";
	private Button but_back, but_submit;
	private EditText edit_user, edit_password, edit_confirmPassword, edit_nickName;
	private ImageView image;

	public Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Tools.closeProgresDialog();
			// 收到消息
			Bundle bundle = msg.getData();
			int status = bundle.getInt(Const.KEY_DATA);
			switch (status) {
			case Const.STATUS_SUCCESS:

				showMessage("注册成功");
				showIntentActivity(LoginActivity.class);
				break;
			case Const.CONNECT_FAILURE:
				showMessage("联网失败");
				break;
			case Const.STATUS_USER_CONFLICT:
				showMessage("用户已存在");
				break;

			}

		}

	};

	protected void showMessage(String text) {
		Toast.makeText(RegisterActivity.this, text, Toast.LENGTH_SHORT).show();
	}

	void showIntentActivity(Class cls) {
		Intent intet = new Intent(RegisterActivity.this, cls);
		startActivity(intet);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		setupView();
		addListener();

	}

	private void addListener() {
		setOnClickListener listener = new setOnClickListener();
		but_back.setOnClickListener(listener);
		but_submit.setOnClickListener(listener);
	}

	private void setupView() {
		but_back = (Button) findViewById(R.id.but_back);
		but_submit = (Button) findViewById(R.id.but_submit);
		edit_user = (EditText) findViewById(R.id.edit_user);
		edit_password = (EditText) findViewById(R.id.edit_password);
		edit_confirmPassword = (EditText) findViewById(R.id.edit_confirm_password);
		edit_nickName = (EditText) findViewById(R.id.edit_nickname);
		image = (ImageView) findViewById(R.id.image_register);

	}

	private class setOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View view) {

			switch (view.getId()) {
			case R.id.but_back:
				Log.i(TAG, "back==============");
				showIntentActivity(LoginActivity.class);
				break;
			case R.id.but_submit:
				Log.i(TAG, "submit==============");
				subMint();

				break;
			}

		}

	}

	private void subMint() {
		try {
			String userName = edit_user.getText().toString();
			String password = edit_password.getText().toString();
			String nickName = edit_nickName.getText().toString();
			String confirmPassword = edit_confirmPassword.getText().toString();
			// 重点是数据检查
			StringBuffer buffer = new StringBuffer();
			if (TextUtils.isEmpty(userName)) {
				buffer.append("用户名不能为空\n");
			}
			if (TextUtils.isEmpty(password)) {
				buffer.append("密码不能为空\n");
			}

			if (!password.equals(confirmPassword)) {
				buffer.append("两次密码不一致");
			}

			if (buffer.toString().length() > 0) {
				Toast.makeText(RegisterActivity.this, buffer.toString(), Toast.LENGTH_SHORT).show();
				return;
			}

			RegisterBiz registerBiz = new RegisterBiz();
			Resources res = getResources();

			Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.default_head);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
			byte[] byteArray = baos.toByteArray();
			UserEntity userEntity = new UserEntity(byteArray, userName, password, nickName);
			registerBiz.register(RegisterActivity.this, userEntity);
			Tools.showProgressDialog(RegisterActivity.this, "正在注册");
			Log.i(TAG, "registerBiz==============");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
